home *** CD-ROM | disk | FTP | other *** search
- {=======================================================================
- File: CUSTOMER.PAS
- Author: Jeffrey L. Darling
- Date established: 06-07-1991
- Latest release: 06-07-1991
- Current Version: 1.00
- =======================================================================}
- Program Customer;
-
- Uses Crt;
-
- type String_10 = String[10];
- Branch=^Treenode;
- Treenode = record
- Account_num : Integer;
- Index : integer;
- Left,Right : Branch
- end; {TreeNode}
- Customer_rec = record
- Name : String[10];
- Account_num : Integer;
- end; {Customer_rec}
-
-
-
- Var Root : Branch;
- Cust_File : File of Customer_rec;
- Cust_rec :Customer_rec;
- Rec_num : Integer;
- flag : char; {End of file flag}
-
-
- procedure makefile;
-
- Begin {Makefile}
- {Erase file if already present}
- assign(Cust_File, 'CUST.DAT');
- {$I-}
- Reset(Cust_File);
- {$I+}
- If IOResult = 0 Then
- begin
- close(Cust_File);
- Erase(Cust_File);
- end;
- assign(Cust_File, 'CUST.DAT');
- rewrite(Cust_File);
- While Upcase(flag) <> 'N' do
- begin
- With Cust_rec do
- begin
- Write('Enter name: ');
- Readln(name);
- Write('Enter Account number: ');
- Readln(Account_num);
- Write('More records? ');
- Readln(Flag);
- end;
- Write(Cust_file, Cust_rec);
- end;
- close(Cust_file);
- end; {Makefile}
-
-
- Procedure BuildTree(var Root : Branch);
-
- var Ancestor : Branch;
-
- Procedure AttachNode(Cust_Rec : Customer_rec;
- Var Ancestor : Branch);
- begin {AttachNode}
- New(Ancestor);
- begin
- Ancestor^.Account_num := Cust_Rec.Account_num;
- Ancestor^.Index := Rec_num;
- Ancestor^.Left := nil; Ancestor^.Right := nil
- end
- end; {AttachNode}
-
- Procedure PutInTree (Cust_rec : Customer_Rec;
-
- var Ancestor : Branch);
-
- begin {PutInTree}
- if Ancestor = nil then
- AttachNode(Cust_rec, Ancestor)
- else if Cust_Rec.Account_num = Ancestor^.Account_num then
- writeln('Error, duplicate account number in file')
- else if Cust_Rec.Account_num < Ancestor^.Account_num then
- PutInTree(Cust_Rec, Ancestor^.Left)
- else PutInTree(Cust_Rec, Ancestor^.Right)
- end; {PutInTree}
-
- begin {BuildTree}
- assign(Cust_File, 'CUST.DAT');
- reset(Cust_File);
- Read(Cust_File, Cust_rec);
- Rec_num := Rec_num + 1;
- AttachNode(Cust_Rec, Root);
- while not EOF(Cust_File) do
- begin
- Read(Cust_File, Cust_Rec);
- Rec_num := Rec_num + 1;
- Ancestor := Root;
- PutInTree(Cust_Rec, Ancestor);
- end;
- end; {BuildTree}
-
- Procedure Traverse(Root : Branch);
-
- begin {Traverse}
- if Root <> nil then
- begin
- Traverse(Root^.Left);
- Write(' ',Root^.Index + 1,' ');
- Write(Root^.Account_num,' ');
- Seek(Cust_File,Root^.Index);
- Read(Cust_file,Cust_rec);
- Writeln(Cust_rec.Name);
- Traverse(Root^.Right);
- end;
- end; {Traverse}
-
-
- begin {MAIN}
- clrscr;
- Flag := 'Y';
- {makefile;} {Use this procedure only to Change the data file}
- flag := 'Y';
- While Upcase(Flag) <> 'N' do
- begin
- Rec_num := -1;
- ClrScr;
- BuildTree(Root);
- Writeln('Index Account Number Name');
- Writeln('~~~~~ ~~~~~~~~~~~~~~ ~~~~');
- Traverse(root);
- close(Cust_file);
- Writeln;
- Write('Run this program again? (y/n) ');readln(flag);
- end;
- end. {MAIN}
-